package dao

import javax.inject.Inject

import models.Tables._
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
import slick.jdbc.JdbcProfile

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

/**
  * Created by yz on 2017/6/1.
  */
class LongRNAProfileDao @Inject()(protected val dbConfigProvider: DatabaseConfigProvider) extends
  HasDatabaseConfigProvider[JdbcProfile] {

  import profile.api._

  def insertAll(longrnaprofiles: Seq[LongrnaprofileRow]): Future[Unit] = db.run(Longrnaprofile ++=
    longrnaprofiles).map(_ => ())

  def deleteAll(): Future[Unit] = db.run(Longrnaprofile.delete).map(_ => ())

  def selectByGeneId(id: String): Future[Seq[LongrnaprofileRow]] = {
    val genes = id.split(",").map(_.trim)
    db.run(Longrnaprofile.
      filter(_.geneid.inSetBind(genes)).result)
  }

  def dropTable(): Future[Unit] = {
    val sqlu = DBIO.seq(
      sqlu"""
          DROP TABLE IF EXISTS longrnaprofile;
        """)
    db.run(sqlu).map(_ => ())
  }


  def createTable(): Future[Unit] = {
    val sqlu = DBIO.seq(
      sqlu"""
         CREATE TABLE `longrnaprofile` (
              `geneId` varchar(255) NOT NULL,
              `sampleName` varchar(255) NOT NULL,
              `value` double NOT NULL,
              `type` varchar(255) NOT NULL,
              PRIMARY KEY (`geneId`,`sampleName`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
        """)
    db.run(sqlu).map(_ => ())
  }


}
